package com.gooapple.notepad.ui;

import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.gooapple.notepad.R;
import com.gooapple.notepad.basic.BasicActivity;
import com.gooapple.notepad.constant.Constant;
import com.gooapple.notepad.model.Picture;
import com.gooapple.notepad.model.PictureList;
import com.gooapple.notepad.util.MediaHelper;
import com.gooapple.notepad.util.PictureListAdapter;
import com.gooapple.notepad.util.UIManager;

public class PictureListActivity extends BasicActivity {
	public static final String TAG = PictureListActivity.class.getName();
	private List<Picture> pictures;
	private ListView pictureList;
	private PictureListAdapter pictureListAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initViews();
		initData();
		setListeners();
	}

	@Override
	protected void initData() {
		pictures = MediaHelper.getPictures(this);
		pictureListAdapter = new PictureListAdapter(pictures, this);
		pictureList.setAdapter(pictureListAdapter);
	}

	@Override
	protected void initViews() {
		setContentView(R.layout.picture_list);
		pictureList = (ListView) findViewById(R.id.picture_list);
		UIManager.showBasicBar(this, R.drawable.icon_picture_reversed,
				getResources().getText(R.string.picture_top_text), null, true,
				true);
	}

	@Override
	protected void setListeners() {
		pictureList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(Constant.ACTION_PICTURE_VIEW);
				PictureList pictureList = new PictureList(pictures);
				intent
						.putExtra(Constant.EXTRA_PICTURE_MODEL_LABEL,
								pictureList);
				intent.putExtra(Constant.EXTRA_PICTURE_POSITION, position);
				startActivityForResult(intent, 101);// request code must >= 0
			}
		});
		pictureList.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				int count = parent.getChildCount();
				Drawable focusD = getResources().getDrawable(
						R.drawable.home_list_deliver_default);
				Drawable defaultD = getResources().getDrawable(
						R.drawable.home_list_deliver_normal);
				for (int i = 0; i < count; i++) {
					View v = parent.getChildAt(i);
					v.setBackgroundDrawable(defaultD);
				}
				view.setBackgroundDrawable(focusD);
			}

			public void onNothingSelected(AdapterView<?> parent) {

			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		int selectPos = data.getIntExtra(Constant.EXTRA_PICTURE_POSITION, 0);
		pictureList.setSelection(selectPos);
		pictureListAdapter.notifyDataSetChanged();
		View view = (View) pictureListAdapter.getItem(selectPos);
		view.setBackgroundResource(R.drawable.home_list_deliver_default);
	}

}
